package com.itheima.web.controller;


import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception4project.CheckItemDeleteFailException;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import io.netty.util.internal.StringUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 检查项请求处理
 */
@RestController
@RequestMapping("/checkItem")
public class CheckItemController {
    /**
     * 注入分布式service注册中心地址
     */
    @Reference
    private CheckItemService checkItemService;

    /**
     * 接收新增检查项请求
     * @param checkItem
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        try {
            checkItemService.add(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        //System.out.println(checkItem.getCode());
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 分也查询全部信息
     * @param queryPageBean
     * @return
     */
    @PostMapping("/pageHelper")
    public PageResult pageHelper(@RequestBody QueryPageBean queryPageBean){
        //System.out.println(queryPageBean.getPageSize());
        try {
            return checkItemService.get4Page(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            //return new PageResult(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return null;
    }

    /**
     * 接收删除检查项请求
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @GetMapping("/delete")
    public Result deleteCheckItem(Integer id){
        //System.out.println("********* "+id+" *********");
        try {
            checkItemService.deleteCheckItem(id);
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof CheckItemDeleteFailException)
                return new Result(false,"删除失败，请解除当前条数据与检查组的关联！");
            else
                return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    /**
     * 接收更新检查项请求信息
     * @param checkItem
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_UPDATE')")
    @PostMapping("/update")
    public Result updateCheckItem(@RequestBody CheckItem checkItem){

        try {
            //调用 service 层update方法更新检查项数据
            checkItemService.updateCheckItem(checkItem);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }
}